﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;

namespace G.Controls
{
    public class Window : ContentControl
    {

        public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(Window), new PropertyMetadata(false, new PropertyChangedCallback(Window.OnIsOpenChanged)));

        public bool IsOpen
        {
            get
            {
                return (bool)base.GetValue(IsOpenProperty);
            }
            set
            {
                base.SetValue(IsOpenProperty, value);
            }
        }

        private static void OnIsOpenChanged(object sender, DependencyPropertyChangedEventArgs A_1)
        {
            Window window = sender as Window;
            if (window != null) window.OnIsOpenChanged();
        }

        public static readonly DependencyProperty HorizontalOffsetPorperty = DependencyProperty.Register("HorizontalOffset", typeof(double), typeof(Window), new PropertyMetadata(0.0));

        public double HorizontalOffset
        {
            get
            {
                return (double)this.GetValue(HorizontalOffsetPorperty);
            }
            set
            {
                this.SetValue(HorizontalOffsetPorperty, value);
            }
        }


        public static readonly DependencyProperty VerticalOffsetPorperty = DependencyProperty.Register("VerticalOffset", typeof(double), typeof(Window), new PropertyMetadata(0.0));

        public double VerticalOffset
        {
            get
            {
                return (double)this.GetValue(VerticalOffsetPorperty);
            }
            set
            {
                this.SetValue(VerticalOffsetPorperty, value);
            }
        }

        protected Popup Popup;
        private Canvas window;
        public Window()
        {
            this.DefaultStyleKey = typeof(Window);
            Popup = new Popup();
            window = new Canvas();
            var bingding = G.Utils.BindingHelp.Create("IsOpen", this);
            bingding.Mode = Windows.UI.Xaml.Data.BindingMode.TwoWay;
            Popup.SetBinding(Windows.UI.Xaml.Controls.Primitives.Popup.IsOpenProperty, bingding);

            this.SetBinding(Canvas.TopProperty, G.Utils.BindingHelp.Create("VerticalOffset", this));
            this.SetBinding(Canvas.LeftProperty, G.Utils.BindingHelp.Create("HorizontalOffset", this));
            Popup.Child = window;
        }

 

        private void OnIsOpenChanged()
        {
            if (this.IsOpen)
            {
                OnOpening();
                if (window.Children.Count == 0)
                {
                    window.Children.Add(this);
                }
                OnOpened();
            }
            else
            {
                OnClosed();
            }
        }


        protected virtual void OnOpening()
        {

        }

        protected virtual void OnOpened()
        {

        }

        protected virtual void OnClosed()
        {
         
        }
    }
}
